package com.jinlingxiu.app.mvp.ui.activity

import android.content.Intent
import android.os.Bundle
import android.view.KeyEvent
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.TimeUtils
import com.jaeger.library.StatusBarUtil
import com.jinlingxiu.app.R
import com.jinlingxiu.app.mvp.contract.PassContract
import com.jinlingxiu.app.mvp.presenter.PassPresenter
import com.jinlingxiu.app.other.db.entity.UserInfoBean
import com.xdja.easymvp.base.BaseActivity
import kotlinx.android.synthetic.main.activity_pass.*
import org.jetbrains.anko.toast
import org.koin.androidx.scope.lifecycleScope

/**
 * 描述:  测试通过页
 * Create by yuanwanli
 * Date 2020/09/11
 */
class PassActivity : BaseActivity<PassPresenter>(), PassContract.View {
    override var mPresenter: PassPresenter? = lifecycleScope.get()
    private var firstTime: Long = 0
    override fun initView(savedInstanceState: Bundle?): Int = R.layout.activity_pass

    override fun initData(savedInstanceState: Bundle?) {
        StatusBarUtil.setTransparent(this)
        StatusBarUtil.setLightMode(this)
        ivBack.setOnClickListener {
            ActivityUtils.finishAllActivities()
            startActivity(Intent(this, LoginActivity::class.java))
        }
        ivSave.setOnClickListener {
            mPresenter?.saveScreen(this)
        }
        mPresenter?.getUserInfo()
    }

    override fun showUser(userInfoBean: UserInfoBean) {
        tvResult.text = String.format(getString(R.string.result_tips), userInfoBean.name)
        tvPassTime.text = String.format(
            getString(R.string.pass_time), TimeUtils.getNowString(
                TimeUtils.getSafeDateFormat("yyyy.MM.dd")
            )
        )
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        val secondTime = System.currentTimeMillis()
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (secondTime - firstTime < 2000) {
                AppUtils.exitApp()
            } else {
                toast("再按一次退出程序")
                firstTime = System.currentTimeMillis()
            }
            return true
        }
        return super.onKeyDown(keyCode, event)
    }
}
